
#include "StateStats.h"
#include "StateManager.h"
#include "StateMainMenu.h"
#include "StateOptions.h"

#include "defs.h"
#include "InputManager.h"
#include "ProgressManager.h"
#include "AudioUtils.h"

StateStats::StateStats()
{
}

StateStats::~StateStats()
{
}

char* intToString(int _iValue, char* _sString)
{
	sprintf(_sString, "%d", _iValue);
	return _sString;
}

void StateStats::loadContent()
{
    m_oBg = new RenderableEntity("MENU_0010_bg", Vector2(320, 480));
    m_oBg->setCentered();
    
    m_oTitleBg = new RenderableEntity("MENU_0009_ribbon", Vector2(320, 870));
    m_oTitleBg->setCentered();
    m_oTitleBg->setLiving(true);
    
    m_oTitleText = new TextEntity("Statistics", FONT_NAME, 60, Vector2(320, 885), 1, 1);
    m_oTitleText->setCentered();
    m_oTitleText->setShadow(true, COLOR_PINK_DARK);
    m_oTitleText->setColor(COLOR_PINK_TEXT);
	
    m_oBabiesMaxIcon = new RenderableEntity("icon_diaper", Vector2(340, 700));
    m_oBabiesTotalIcon = new RenderableEntity("icon_diaper", Vector2(340, 640));
    m_oCoinsIcon = new RenderableEntity("icon_babycoin", Vector2(340, 580));
    m_oCoinsTotalIcon = new RenderableEntity("icon_babycoin", Vector2(340, 520));
    m_oCoinsSpentIcon = new RenderableEntity("icon_babycoin", Vector2(340, 460));
    m_oGamesIcon = new RenderableEntity("Type2", Vector2(340, 400));
    m_oPrizesIcon = new RenderableEntity("shit", Vector2(340, 340));
    m_oComboMaxIcon = new RenderableEntity("chainHolder", Vector2(340, 280));

    m_oBabiesMaxIcon->setCentered();
    m_oBabiesTotalIcon->setCentered();
    m_oCoinsIcon->setCentered();
    m_oCoinsTotalIcon->setCentered();
    m_oCoinsSpentIcon->setCentered();
    m_oGamesIcon->setCentered();
    m_oPrizesIcon->setCentered();
    m_oComboMaxIcon->setCentered();
    
    m_oBabiesMaxIcon->setLiving(true);
    m_oBabiesTotalIcon->setLiving(true);
    m_oCoinsIcon->setLiving(true);
    m_oCoinsTotalIcon->setLiving(true);
    m_oCoinsSpentIcon->setLiving(true);
    m_oGamesIcon->setLiving(true);
    m_oPrizesIcon->setLiving(true);
    m_oComboMaxIcon->setLiving(true);
    
    m_oBabiesMaxIcon->setScale(Vector2(0.4f, 0.4f));
    m_oBabiesTotalIcon->setScale(Vector2(0.4f, 0.4f));    
    m_oCoinsIcon->setScale(Vector2(0.4f, 0.4f));
    m_oCoinsTotalIcon->setScale(Vector2(0.4f, 0.4f));
    m_oCoinsSpentIcon->setScale(Vector2(0.4f, 0.4f));
    m_oGamesIcon->setScale(Vector2(0.4f, 0.4f));
    m_oPrizesIcon->setScale(Vector2(0.4f, 0.4f));
    m_oComboMaxIcon->setScale(Vector2(0.35f, 0.35f));
    
    m_tBabiesMax = new TextEntity("Max Babies", FONT_NAME, 36, Vector2(305, 700), 2, 1);
    m_tBabiesTotal = new TextEntity("Total Babies", FONT_NAME, 36, Vector2(305, 640), 2, 1);
    m_tCoins = new TextEntity("Coins", FONT_NAME, 36, Vector2(305, 580), 2, 1);
    m_tCoinsTotal = new TextEntity("Total Coins", FONT_NAME, 36, Vector2(305, 520), 2, 1);
    m_tCoinsSpent = new TextEntity("Spent Coins", FONT_NAME, 36, Vector2(305, 460), 2, 1);
    m_tGames = new TextEntity("Games Played", FONT_NAME, 36, Vector2(305, 400), 2, 1);
    m_tPrizes = new TextEntity("Prizes Collected", FONT_NAME, 36, Vector2(305, 340), 2, 1);
    m_tComboMax = new TextEntity("Max Chain", FONT_NAME, 36, Vector2(305, 280), 2, 1);
	
    m_tBabiesMax->setCenter(Vector2(1.0f, 0.5f));
    m_tBabiesTotal->setCenter(Vector2(1.0f, 0.5f));
    m_tCoins->setCenter(Vector2(1.0f, 0.5f));
    m_tCoinsTotal->setCenter(Vector2(1.0f, 0.5f));
    m_tCoinsSpent->setCenter(Vector2(1.0f, 0.5f));
    m_tGames->setCenter(Vector2(1.0f, 0.5f));
    m_tPrizes->setCenter(Vector2(1.0f, 0.5f));
    m_tComboMax->setCenter(Vector2(1.0f, 0.5f));
	
    m_tBabiesMax->setShadow();
    m_tBabiesTotal->setShadow();
    m_tCoins->setShadow();
    m_tCoinsTotal->setShadow();
    m_tCoinsSpent->setShadow();
    m_tGames->setShadow();
    m_tPrizes->setShadow();
    m_tComboMax->setShadow();
	
    m_tBabiesMax_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 700), 0, 1);
    m_tBabiesTotal_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 640), 0, 1);
    m_tCoins_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 580), 0, 1);
    m_tCoinsTotal_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 520), 0, 1);
    m_tCoinsSpent_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 460), 0, 1);
    m_tGames_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 400), 0, 1);
    m_tPrizes_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 340), 0, 1);
    m_tComboMax_value = new TextEntity("0", FONT_NAME, 36, Vector2(380, 280), 0, 1);

    m_tBabiesMax_value->setCenter(Vector2(0.0f, 0.5f));
    m_tBabiesTotal_value->setCenter(Vector2(0.0f, 0.5f));
    m_tCoins_value->setCenter(Vector2(0.0f, 0.5f));
    m_tCoinsTotal_value->setCenter(Vector2(0.0f, 0.5f));
    m_tCoinsSpent_value->setCenter(Vector2(0.0f, 0.5f));
    m_tGames_value->setCenter(Vector2(0.0f, 0.5f));
    m_tPrizes_value->setCenter(Vector2(0.0f, 0.5f));
    m_tComboMax_value->setCenter(Vector2(0.0f, 0.5f));
	
    m_tBabiesMax_value->setShadow();
    m_tBabiesTotal_value->setShadow();
    m_tCoins_value->setShadow();
    m_tCoinsTotal_value->setShadow();
    m_tCoinsSpent_value->setShadow();
    m_tGames_value->setShadow();
    m_tPrizes_value->setShadow();
    m_tComboMax_value->setShadow();

    m_oBack = new RenderableEntity("MENU_0008_back", Vector2(80, 80));
    m_oBack->setCentered();
    m_oBack->changeColorWhenClicked(true, COLOR_BLACK);
    m_oBack->setLiving(true);
    
    
	m_eEnter = new TimeLineEffect(false, false);
    m_eEnter->addInEffect(m_oTitleBg);
    m_eEnter->addInEffect(m_oTitleText);
    m_eEnter->addInEffect(m_oBack);

    m_eEnter->addInEffect(m_oBabiesMaxIcon);
    m_eEnter->addInEffect(m_oBabiesTotalIcon);
    m_eEnter->addInEffect(m_oCoinsIcon);
    m_eEnter->addInEffect(m_oCoinsTotalIcon);
    m_eEnter->addInEffect(m_oCoinsSpentIcon);
    m_eEnter->addInEffect(m_oGamesIcon);
    m_eEnter->addInEffect(m_oPrizesIcon);
    m_eEnter->addInEffect(m_oComboMaxIcon);
    
    m_eEnter->addInEffect(m_tBabiesMax);
    m_eEnter->addInEffect(m_tBabiesTotal);
    m_eEnter->addInEffect(m_tCoins);
    m_eEnter->addInEffect(m_tCoinsTotal);
    m_eEnter->addInEffect(m_tCoinsSpent);
    m_eEnter->addInEffect(m_tGames);
    m_eEnter->addInEffect(m_tPrizes);
    m_eEnter->addInEffect(m_tComboMax);

    m_eEnter->addInEffect(m_tBabiesMax_value);
    m_eEnter->addInEffect(m_tBabiesTotal_value);
    m_eEnter->addInEffect(m_tCoins_value);
    m_eEnter->addInEffect(m_tCoinsTotal_value);
    m_eEnter->addInEffect(m_tCoinsSpent_value);
    m_eEnter->addInEffect(m_tGames_value);
    m_eEnter->addInEffect(m_tPrizes_value);
    m_eEnter->addInEffect(m_tComboMax_value);

    
	m_eExit = new TimeLineEffect(false, false);
    m_eExit->addOutEffect(m_oTitleBg);
    m_eExit->addOutEffect(m_oTitleText);
    m_eExit->addOutEffect(m_oBack);
	
    m_eExit->addOutEffect(m_oBabiesMaxIcon);
    m_eExit->addOutEffect(m_oBabiesTotalIcon);
    m_eExit->addOutEffect(m_oCoinsIcon);
    m_eExit->addOutEffect(m_oCoinsTotalIcon);
    m_eExit->addOutEffect(m_oCoinsSpentIcon);
    m_eExit->addOutEffect(m_oGamesIcon);
    m_eExit->addOutEffect(m_oPrizesIcon);
    m_eExit->addOutEffect(m_oComboMaxIcon);
    
    m_eExit->addOutEffect(m_tBabiesMax);
    m_eExit->addOutEffect(m_tBabiesTotal);
    m_eExit->addOutEffect(m_tCoins);
    m_eExit->addOutEffect(m_tCoinsTotal);
    m_eExit->addOutEffect(m_tCoinsSpent);
    m_eExit->addOutEffect(m_tGames);
    m_eExit->addOutEffect(m_tPrizes);
    m_eExit->addOutEffect(m_tComboMax);
    
    m_eExit->addOutEffect(m_tBabiesMax_value);
    m_eExit->addOutEffect(m_tBabiesTotal_value);
    m_eExit->addOutEffect(m_tCoins_value);
    m_eExit->addOutEffect(m_tCoinsTotal_value);
    m_eExit->addOutEffect(m_tCoinsSpent_value);
    m_eExit->addOutEffect(m_tGames_value);
    m_eExit->addOutEffect(m_tPrizes_value);
    m_eExit->addOutEffect(m_tComboMax_value);
}

void StateStats::unloadContent()
{
}

void StateStats::enter()
{
    char tiChar[10];
    m_tBabiesMax_value->setText(intToString(ProgressManager::instance().getBabiesFedMax(), &tiChar[0]));
    m_tBabiesTotal_value->setText(intToString(ProgressManager::instance().getBabiesFedTotal(), &tiChar[0]));
    m_tCoins_value->setText(intToString(ProgressManager::instance().getCoins(), &tiChar[0]));
    m_tCoinsTotal_value->setText(intToString(ProgressManager::instance().getCoinsTotal(), &tiChar[0]));
    m_tCoinsSpent_value->setText(intToString(ProgressManager::instance().getCoinsSpent(), &tiChar[0]));
    m_tGames_value->setText(intToString(ProgressManager::instance().getGamesTotal(), &tiChar[0]));
    m_tPrizes_value->setText(intToString(ProgressManager::instance().getPrizesTotal(), &tiChar[0]));
    m_tComboMax_value->setText(intToString(ProgressManager::instance().getChainMax(), &tiChar[0]));

	m_eState = Enter;
	m_eEnter->start(true);
}

void StateStats::update(float dt)
{
	switch(m_eState)
	{
		case Enter:
			if(m_eEnter->isFinished())
			{
				m_eState = Idle;
			}
			break;

		case Idle:
            m_oTitleBg->update();
            m_oTitleText->setScale(m_oTitleBg->getScale());
            
            m_oBack->update();

            m_oComboMaxIcon->setRotation(-SB::gameTime * ProgressManager::instance().getSavedData().iChainMax * 0.1);
            
            m_oBabiesMaxIcon->update();
            m_oBabiesTotalIcon->update();
            m_oCoinsIcon->update();
            m_oCoinsTotalIcon->update();
            m_oCoinsSpentIcon->update();
            m_oGamesIcon->update();
            m_oPrizesIcon->update();
            m_oComboMaxIcon->update();
            
            m_oTitleText->setScale(m_oTitleBg->getScale());
            
			if(m_oBack->isClicked())
			{
				m_eExit->start(true);
				m_eState = Exit;
			}
			break;
            
		case Exit:
			if(m_eExit->isFinished())
			{
				StateManager::instance().setState(StateOptions::instance());
			}
			break;
	}
}

void StateStats::render()
{
    m_oBg->render();
    m_oTitleBg->render();
    m_oTitleText->render();

    m_oBabiesMaxIcon->render();
    m_oBabiesTotalIcon->render();
    m_oCoinsIcon->render();
    m_oCoinsTotalIcon->render();
    m_oCoinsSpentIcon->render();
    m_oGamesIcon->render();
    m_oPrizesIcon->render();
    m_oComboMaxIcon->render();
    
    m_tBabiesMax->render();
    m_tBabiesTotal->render();
    m_tCoins->render();
    m_tCoinsTotal->render();
    m_tCoinsSpent->render();
    m_tGames->render();
    m_tPrizes->render();
    m_tComboMax->render();
    
    m_tBabiesMax_value->render();
    m_tBabiesTotal_value->render();
    m_tCoins_value->render();
    m_tCoinsTotal_value->render();
    m_tCoinsSpent_value->render();
    m_tGames_value->render();
    m_tPrizes_value->render();
    m_tComboMax_value->render();
    
    m_oBack->render();    
}

void StateStats::exit()
{
}
